{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://stackoverflow.com/questions/48878855/python-base64-encoding-a-list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x80\\x05\\x95\\x87\\x00\\x00\\x00\\x00\\x00\\x00\\x00]\\x94(K\\x01\\x8c\\x05hello\\x94}\\x94(\\x8c\\x01a\\x94]\\x94(K\\x01G@\\x00\\x00\\x00\\x00\\x00\\x00\\x00K\\x03\\x8c\\x08builtins\\x94\\x8c\\x07complex\\x94\\x93\\x94G@\\x10\\x00\\x00\\x00\\x00\\x00\\x00G@\\x18\\x00\\x00\\x00\\x00\\x00\\x00\\x86\\x94R\\x94e\\x8c\\x01b\\x94\\x8c\\x10character string\\x94C\\x0bbyte string\\x94\\x86\\x94\\x8c\\x01c\\x94\\x8f\\x94(\\x89\\x88N\\x90ue.'\n",
      "b'gAWVhwAAAAAAAABdlChLAYwFaGVsbG+UfZQojAFhlF2UKEsBR0AAAAAAAAAASwOMCGJ1aWx0aW5zlIwHY29tcGxleJSTlEdAEAAAAAAAAEdAGAAAAAAAAIaUUpRljAFilIwQY2hhcmFjdGVyIHN0cmluZ5RDC2J5dGUgc3RyaW5nlIaUjAFjlI+UKImITpB1ZS4='\n",
      "b'\\x80\\x05\\x95\\x87\\x00\\x00\\x00\\x00\\x00\\x00\\x00]\\x94(K\\x01\\x8c\\x05hello\\x94}\\x94(\\x8c\\x01a\\x94]\\x94(K\\x01G@\\x00\\x00\\x00\\x00\\x00\\x00\\x00K\\x03\\x8c\\x08builtins\\x94\\x8c\\x07complex\\x94\\x93\\x94G@\\x10\\x00\\x00\\x00\\x00\\x00\\x00G@\\x18\\x00\\x00\\x00\\x00\\x00\\x00\\x86\\x94R\\x94e\\x8c\\x01b\\x94\\x8c\\x10character string\\x94C\\x0bbyte string\\x94\\x86\\x94\\x8c\\x01c\\x94\\x8f\\x94(\\x89\\x88N\\x90ue.'\n",
      "[1, 'hello', {'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string'), 'c': {False, True, None}}]\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "import base64\n",
    "\n",
    "data = [\n",
    "    1,\n",
    "    \"hello\",\n",
    "    {\n",
    "        'a': [1, 2.0, 3, 4+6j],\n",
    "        'b': (\"character string\", b\"byte string\"),\n",
    "        'c': set([None, True, False])\n",
    "    }\n",
    "]\n",
    "\n",
    "pstr = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)\n",
    "bstr = base64.b64encode(pstr)\n",
    "print(pstr)\n",
    "print(bstr)\n",
    "\n",
    "pstr = base64.b64decode(bstr)\n",
    "print(pstr)\n",
    "new_data = pickle.loads(pstr)\n",
    "print(new_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import base64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [{\"class_index\": class_index, \"confidence\": confidence, \"box\": [xmin, ymin, xmax, ymax]}， {}] box为int类型\n",
    "\n",
    "data = [\n",
    "    {\"class_index\": 0, \"confidence\": 0.9, \"box\": [1, 2, 100, 200]},\n",
    "    {\"class_index\": 1, \"confidence\": 0.8, \"box\": [100, 12, 300, 400]},\n",
    "    {\"class_index\": 1, \"confidence\": 0.7, \"box\": [102, 200, 300, 520]},\n",
    "]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 编码 pickle -> base64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\x80\\x04\\x95\\x87\\x00'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pickle_data = pickle.dumps(data)\n",
    "pickle_data[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'gASVh'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接将imencode的结果进行base64编码\n",
    "data_64 = base64.b64encode(pickle_data)\n",
    "data_64[:5]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 解码 base64 -> pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\x80\\x04\\x95\\x87\\x00'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 解码\n",
    "data_decode = base64.b64decode(data_64)\n",
    "data_decode[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'class_index': 0, 'confidence': 0.9, 'box': [1, 2, 100, 200]},\n",
       " {'class_index': 1, 'confidence': 0.8, 'box': [100, 12, 300, 400]},\n",
       " {'class_index': 1, 'confidence': 0.7, 'box': [102, 200, 300, 520]}]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_res = pickle.loads(data_decode)\n",
    "data_res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data == data_res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
